package app.foodnote;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.app.SearchManager;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

import app.foodnote.db.FoodNotesDataSource;
import app.foodnote.db.MySQLiteHelper;


public class FoodPlaces extends ListActivity {
	 private FoodNotesDataSource datasource;
	 Cursor cursor;
	 AlertDialog.Builder viewDetail;
	 ListView listView;
	 @Override
	 public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setTitle("Danh sách món ăn");
	        setContentView(R.layout.form_places);
	        
	        datasource = new FoodNotesDataSource(this);
	        // Search foods
			Intent intentSearch = getIntent();
			if(Intent.ACTION_SEARCH.equals(intentSearch.getAction())){
				
				String query = intentSearch.getStringExtra(SearchManager.QUERY);
				doMySearch(query);
			} else {
				
			}

			
	 }
	private void doMySearch(String query) {
		// TODO Auto-generated method stub
		
	}
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		String Id = cursor.getString(cursor.getColumnIndex(MySQLiteHelper.COLUMN_ID));
		String name = cursor.getString(cursor.getColumnIndex(MySQLiteHelper.FOOD_NAME));
		String address = cursor.getString(cursor.getColumnIndex(MySQLiteHelper.ADDRESS));
		String city = cursor.getString(cursor.getColumnIndex(MySQLiteHelper.CITIES));
		String phonenumber = cursor.getString(cursor.getColumnIndex(MySQLiteHelper.PHONE_NUMBER));
		String othernote = cursor.getString(cursor.getColumnIndex(MySQLiteHelper.OTHER_NOTES));
		viewDetail = new AlertDialog.Builder(this);
		viewDetail.setTitle("Chi Tiết Món Ăn");
		viewDetail.setMessage("Id : " + Id + "\nTên món ăn : " + name + "\nĐịa chỉ : " + address + " " + city
				+ "\nĐiện thoại : " + phonenumber + "\nGhi chú : " + othernote);
		viewDetail.setPositiveButton("Đồng Ý",
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						dialog.dismiss();
					}
				});
		viewDetail.setNegativeButton("Cập Nhật", new DialogInterface.OnClickListener() {
			
			public void onClick(DialogInterface dialog, int which) {
				Intent intentFoodPlace = new Intent(FoodPlaces.this, FoodAdd.class);
				Boolean send = true;
				intentFoodPlace.putExtra("send", send);
				
				intentFoodPlace.putExtra("Id", cursor.getString(cursor.getColumnIndex(MySQLiteHelper.COLUMN_ID)));
				intentFoodPlace.putExtra("name", cursor.getString(cursor.getColumnIndex(MySQLiteHelper.FOOD_NAME)));
				intentFoodPlace.putExtra("address", cursor.getString(cursor.getColumnIndex(MySQLiteHelper.ADDRESS)));
				intentFoodPlace.putExtra("city", cursor.getString(cursor.getColumnIndex(MySQLiteHelper.CITIES)));
				intentFoodPlace.putExtra("phonenumber", cursor.getString(cursor.getColumnIndex(MySQLiteHelper.PHONE_NUMBER)));
				intentFoodPlace.putExtra("othernote", cursor.getString(cursor.getColumnIndex(MySQLiteHelper.OTHER_NOTES)));
				startActivity(intentFoodPlace);			
			}
		});
		viewDetail.show();
	}
	@Override
	protected void onResume() {
		datasource.openToRead();
		
	   /* List<FoodNote> values = datasource.getAllFoodNotes();
        ArrayAdapter<FoodNote> adapter = new ArrayAdapter<FoodNote>(this,
        		android.R.layout.simple_list_item_multiple_choice, values);*/
	    cursor = datasource.getAllEntry();
	    startManagingCursor(cursor);
	    String[] from = new String[] {MySQLiteHelper.FOOD_NAME, MySQLiteHelper.ADDRESS};
	    int[] to = new int[] {R.id.foodname, R.id.address};
	    SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.row, cursor, from, to);
        setListAdapter(adapter);

        datasource.close();
	    super.onResume();

	  }

	  @Override
	  protected void onPause() {
		  datasource.close();
	      super.onPause();
	  }
	  @Override
	  protected void onDestroy(){
		  super.onDestroy();
		  datasource.close();
	  }
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.form_places_option, menu);
		return true;
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item){
		if(item.getItemId()==R.id.add){
			startActivity(new Intent(FoodPlaces.this, FoodAdd.class));
			return true;
		}
		else if(item.getItemId()==R.id.setting){
			startActivity(new Intent(FoodPlaces.this, formsort.class));
			return true;
		}
		else if(item.getItemId() == R.id.search) {
			onSearchRequested();
			return true;
		}
		return super.onOptionsItemSelected(item);
		
	}

}
